package cn.saas.jingtandata;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;


import java.util.List;

/**
 * 单纯字符串适配器
 *
 * @author A18ccms a18ccms_gmail_com
 * @ClassName: SpinnerCustomAdapter
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2015年10月13日 上午10:59:17
 */
public class FileBrowseItemAdapter extends ArrayAdapter<FileInfoBean> {
    /**
     * 文件
     */
    final int FILE = 0;
    /**
     * 文件夹
     */
    final int FLODER = 1;
    /**
     * sd 卡
     */
    final int SDCARD = 2;

    /**
     * 数据集
     */
    private List<FileInfoBean> mData;
    /**
     * 上下文
     */
    private Context mContext;
    /**
     * 布局文件
     */
    private int mResource;

    /**
     * 构造函数
     * <p>
     * Title:
     * </p>
     * <p>
     * Description:
     * </p>
     *
     * @param context
     * @param resource
     * @param objects
     */
    public FileBrowseItemAdapter(Context context, int resource,
                                 List<FileInfoBean> objects) {
        super(context, resource, objects);
        this.mContext = context;
        mResource = resource;
        mData = objects;
    }

    @Override
    public int getCount() {

        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View retView;
        final FileInfoBean bean;
        if (mData != null && mData.size() > position) {
            bean = mData.get(position);
            if (bean != null) {
                ViewHolder holder;
                if (convertView != null) {
                    holder = (ViewHolder) convertView.getTag();
                    retView = convertView;
                } else {
                    holder = new ViewHolder();
                    String inflater = Context.LAYOUT_INFLATER_SERVICE;
                    LayoutInflater layoutInflater = (LayoutInflater) mContext
                            .getSystemService(inflater);
                    retView = layoutInflater.inflate(mResource, null);
                    holder.selectBox = (CheckBox) retView
                            .findViewById(R.id.select_box);
                    holder.fileIco = (ImageView) retView
                            .findViewById(R.id.file_ico);
                    holder.fileName = (TextView) retView
                            .findViewById(R.id.file_name);
                    // 设置选中事件
                    holder.selectBox
                            .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                                @Override
                                public void onCheckedChanged(
                                        CompoundButton buttonView,
                                        boolean isChecked) {

                                    if (buttonView.getTag() instanceof FileInfoBean) {
                                        ((FileInfoBean) buttonView.getTag())
                                                .setSelect(isChecked);
                                    }
                                }
                            });
                }

                switch (bean.getFileType()) {
                    case FILE:
                        holder.fileIco.setImageResource(R.drawable.file_type_file);
                        holder.selectBox.setVisibility(View.VISIBLE);
                        break;
                    case FLODER:
                        holder.fileIco
                                .setImageResource(R.drawable.file_type_folder);
                        holder.selectBox.setVisibility(View.VISIBLE);
                        break;
                    case SDCARD:
                        holder.fileIco.setImageResource(R.drawable.file_type_sd);
                        holder.selectBox.setVisibility(View.GONE);
                        break;
                    default:
                        break;
                }
                holder.fileName.setText(bean.getFileName() != null ? bean
                        .getFileName() : "");
                // 清空原tag
                holder.selectBox.setTag(null);
                // 设置值
                holder.selectBox.setChecked(bean.isSelect());
                // 设置标记
                holder.selectBox.setTag(bean);

                retView.setTag(holder);
                return retView;
            }
        }

        return super.getView(position, convertView, parent);
    }

    /**
     * @param mData 要设置的 mData {@link #mData}
     */
    public void setData(List<FileInfoBean> mData) {
        this.mData = mData;
        notifyDataSetChanged();
    }

    /**
     * 视图控件
     *
     * @author zoe
     * @ClassName: ViewHolder
     * @Description: TODO(这里用一句话描述这个类的作用)
     * @date 2015-1-5 上午10:03:31
     */
    public class ViewHolder {
        /**
         * 批次标题
         */
        public CheckBox selectBox;
        /**
         * 文件图标
         */
        public ImageView fileIco;
        /**
         * 文件名称
         */
        public TextView fileName;

    }
}
